package fairygui.editor.plugin
{
   import fairygui.GRoot;
   import fairygui.editor.Consts;
   import fairygui.editor.EditorWindow;
   import fairygui.editor.FairyGUIEditor;
   import fairygui.editor.loader.EasyLoader;
   import fairygui.editor.loader.LoaderExt;
   import fairygui.editor.utils.BulkTasks;
   import fairygui.utils.ZipReader;
   import flash.filesystem.File;
   import flash.system.ApplicationDomain;
   import flash.system.LoaderContext;
   import flash.utils.ByteArray;

    import plugin.AutoGenerateCodePlugin;

    public class PlugInManager implements IFairyGUIEditor
   {
       
      
      public var editorWindow:EditorWindow;
      
      public var allPlugins:Vector.<PlugInInfo>;
      
      public var publishHandlers:Vector.<IPublishHandler>;
      
      public var comExtensions:Object;
      
      public var comExtensionIDs:Array;
      
      public var comExtensionNames:Array;
      
      private var _loadTasks:BulkTasks;
      
      public function PlugInManager(param1:EditorWindow)
      {
         super();
         this.editorWindow = param1;
         this.allPlugins = new Vector.<PlugInInfo>();
         this.publishHandlers = new Vector.<IPublishHandler>();
         this.comExtensions = {};
         this.comExtensionIDs = ["","Button","Label","ProgressBar","ScrollBar","Slider","ComboBox"];
         this.comExtensionNames = [Consts.g.text139,Consts.g.text140,Consts.g.text141,Consts.g.text142,Consts.g.text143,Consts.g.text144,Consts.g.text145];

          publishHandlers.push(new AutoGenerateCodePlugin(this));
      }
      
      public function get project() : IEditorUIProject
      {
         return this.editorWindow.project;
      }
      
      public function getPackage(param1:String) : IEditorUIPackage
      {
         return this.editorWindow.project.getPackageByName(param1);
      }
      
      public function get language() : String
      {
         return FairyGUIEditor.language;
      }
      
      public function get groot() : GRoot
      {
         return this.editorWindow.groot;
      }
      
      public function get menuBar() : IMenuBar
      {
         return this.editorWindow.mainPanel.menuBar;
      }
      
      public function alert(param1:String) : void
      {
         this.editorWindow.alert(param1);
      }
      
      public function registerPublishHandler(param1:IPublishHandler) : void
      {
         this.publishHandlers.push(param1);
      }
      
      public function registerComponentExtension(param1:String, param2:String, param3:String) : void
      {
         var _loc4_:String = null;
         if(this.comExtensions[param2])
         {
            this.editorWindow.alert("Component extension \'" + param2 + "\' already exists!");
            return;
         }
         if(param3 != null)
         {
            _loc4_ = param3.substr(1);
         }
         else
         {
            _loc4_ = null;
         }
         if(_loc4_ != null && this.comExtensionIDs.indexOf(_loc4_) == -1)
         {
            this.editorWindow.alert("Component extension \'" + param3 + "\' does not exist!");
            return;
         }
         this.comExtensions[param2] = {
            "name":param1,
            "className":param2,
            "superClassName":_loc4_
         };
         this.comExtensionIDs.push(param2);
         this.comExtensionNames.push(param1);
      }
      
      public function load(param1:Function = null) : void
      {
         var file:File = null;
         var pluginFolder:File = null;
         var callback:Function = param1;
         this.reset();
         try
         {
            pluginFolder = new File(new File("app:/plugins").nativePath);
            if(!pluginFolder.exists)
            {
               pluginFolder.createDirectory();
            }
         }
         catch(e:Error)
         {
            if(callback != null)
            {
               callback();
            }
            return;
         }
         var files:Array = pluginFolder.getDirectoryListing();
         var task:Function = function():void
         {
            var _loc1_:File = File(_loadTasks.taskData);
            EasyLoader.load(_loc1_.url,{"name":_loc1_.name},__loadSwcCompleted);
         };
         this._loadTasks = new BulkTasks(5);
         for each(file in files)
         {
            if(!(file.isDirectory || file.extension.toLowerCase() != "swc"))
            {
               this._loadTasks.addTask(task,file);
            }
         }
         this._loadTasks.start(callback);
      }
      
      private function reset() : void
      {
         var plugin:PlugInInfo = null;
         for each(plugin in this.allPlugins)
         {
            try
            {
//               plugin.entry.dispose();
            }
            catch(err:Error)
            {
               continue;
            }
         }
         if(this.allPlugins.length > 0)
         {
            this.editorWindow.mainPanel.menuBar.clearAll();
            this.editorWindow.mainPanel.setupMenuBar();
         }
         this.allPlugins.length = 0;
//         this.publishHandlers.length = 0;
         this.comExtensionIDs.length = 7;
         this.comExtensionNames.length = 7;
         this.comExtensions = {};
      }
      
      private function __loadSwcCompleted(param1:LoaderExt) : void
      {
         if(!param1.content)
         {
            this._loadTasks.finishItem();
            return;
         }
         var _loc2_:ZipReader = new ZipReader(param1.content);
         var _loc3_:ByteArray = _loc2_.getEntryData("library.swf");
         var _loc4_:LoaderContext = new LoaderContext(false,new ApplicationDomain(ApplicationDomain.currentDomain));
         _loc4_.allowCodeImport = true;
         _loc4_.allowLoadBytesCodeExecution = true;
         EasyLoader.load("",{
            "rawContent":_loc3_,
            "context":_loc4_,
            "name":param1.props.name
         },this.__decodeSwcCompleted);
      }
      
      private function __decodeSwcCompleted(param1:LoaderExt) : void
      {
         var name:String = null;
         var clsName:String = null;
         var pos:int = 0;
         var info:PlugInInfo = null;
         var l:LoaderExt = param1;
         this._loadTasks.finishItem();
         if(!l.content)
         {
            return;
         }
         var mainClass:Object = null;
         var names:Vector.<String> = l.applicationDomain.getQualifiedDefinitionNames();
         for each(name in names)
         {
            pos = name.indexOf("::");
            if(pos == -1)
            {
               clsName = name;
            }
            else
            {
               clsName = name.substr(pos + 2);
            }
            if(clsName == "PlugInMain")
            {
               mainClass = l.applicationDomain.getDefinition(name);
            }
         }
         if(!mainClass)
         {
            return;
         }
         try
         {
            info = new PlugInInfo();
            info.name = l.props.name;
            info.entry = new mainClass(this);
            this.allPlugins.push(info);
            return;
         }
         catch(err:Error)
         {
            return;
         }
      }
   }
}
